<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestAdPage extends PHPUnit_Framework_TestCase {
	
	function test_page_inc_check_access() {
		$page = new Page();

		$add_item_page = new AdPage();
		$mock_access = $this->getMock('ItemFunctions');
		$mock_access->expects($this->once())
				->method('item_page_inc_check_access')
				->with($page);
		$add_item_page->set_item_functions($mock_access);

		$add_item_page->page_inc_check_access($page);
	}

	function test_page_inc_execute_action() {

		$expected_action = 'action';

		$page = new Page();

		$ad_page = new AdPage($page);

		$mock_item_functions = $this->getMock('ItemFunctions');	
		$mock_item_functions->expects($this->once())
				->method('item_page_inc_execute_action')
				->with($page, $expected_action, 'article');
		$ad_page->set_item_functions($mock_item_functions);

		$ad_page->page_inc_execute_action($page, $expected_action);
	}

	function test_page_inc_execute_with_no_results() {
		$expected_id = 12345;
		$expected_sql_query = "sql query";
		$expected_sql_result = "sql result";
		$expected_exception = new Exception();

		$page = new Page();
		$page->id = $expected_id;

		$ad_page = new AdPage();

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(array(
							array('*'),
							array('*'),
							array(),
							"M1.nick AS nick_author, M2.nick AS nick_editor"), 
				   array('ITEM','AD','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), 
				   "ITEM.item_id = $expected_id AND ITEM.item_id = AD.item_id AND ITEM.author = M1.user_id AND ITEM.editor = M2.user_id")
				->will($this->returnValue($expected_sql_query));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql_query)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_num_rows')
				->with($expected_sql_result)
				->will($this->returnValue(0));
		$ad_page->set_db_accessor($mock_db_access);

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with(404)
				->will($this->throwException($expected_exception));
		$ad_page->set_error_navigation($mock_error_nav);

		try {
			$ad_page->page_inc_execute($page);
		} catch (Exception $ex) {
			//ignore
		}
	}

	function test_page_inc_execute_with_deleted_ad() {
		$expected_id = 12345;
		$expected_sql_query = "sql query";
		$expected_sql_result = "sql result";
		$expected_exception = new Exception();
		$expected_region_id = 23456;
		$expected_region_tree = 'expected region tree';
		$expected_game_id = 34567;
		$expected_game_tree = array($expected_game_id => 'some value');
		$expected_ad = array('deleted' => true, 'region_id' => $expected_region_id, 'game_id' => $expected_game_id);

		$page = new Page();
		$page->id = $expected_id;

		$ad_page = new AdPage();

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_region_tree')
				->with($expected_region_id)
				->will($this->returnValue($expected_region_tree));
		$ad_page->set_region_manager($mock_region_manager);

		$mock_game_manager = $this->getMock('GameManager');
		$mock_game_manager->expects($this->once())
				->method('game_get_games')
				->with()
				->will($this->returnValue($expected_game_tree));
		$ad_page->set_game_manager($mock_game_manager);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(array(
							array('*'),
							array('*'),
							array(),
							"M1.nick AS nick_author, M2.nick AS nick_editor"), 
				   array('ITEM','AD','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), 
				   "ITEM.item_id = $expected_id AND ITEM.item_id = AD.item_id AND ITEM.author = M1.user_id AND ITEM.editor = M2.user_id")
				->will($this->returnValue($expected_sql_query));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql_query)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_num_rows')
				->with($expected_sql_result)
				->will($this->returnValue(1));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue($expected_ad));
		$ad_page->set_db_accessor($mock_db_access);

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->once())
				->method('errorpage')
				->with(404)
				->will($this->throwException($expected_exception));
		$ad_page->set_error_navigation($mock_error_nav);

		try {
			$ad_page->page_inc_execute($page);
		} catch (Exception $ex) {
			//ignore
		}
	}

	function test_page_inc_execute_with_ad() {
		$expected_id = 12345;
		$expected_sql_query = "sql query";
		$expected_sql_result = "sql result";
		$expected_title = 'some title';
		$expected_author = 'some author';
		$expected_exception = new Exception();
		$expected_region_id = 23456;
		$expected_region_tree = 'expected region tree';
		$expected_game_id = 34567;
		$expected_game_tree = array($expected_game_id => 'some value');
		$expected_ad = array('deleted' => false, 
				'region_id' => $expected_region_id, 
				'game_id' => $expected_game_id,
				'title' => $expected_title, 
				'author' => $expected_author);

		$page = new Page();
		$page->id = $expected_id;

		$ad_page = new AdPage();

		$mock_region_manager = $this->getMock('RegionManager');
		$mock_region_manager->expects($this->once())
				->method('region_get_region_tree')
				->with($expected_region_id)
				->will($this->returnValue($expected_region_tree));
		$ad_page->set_region_manager($mock_region_manager);

		$mock_game_manager = $this->getMock('GameManager');
		$mock_game_manager->expects($this->once())
				->method('game_get_games')
				->with()
				->will($this->returnValue($expected_game_tree));
		$ad_page->set_game_manager($mock_game_manager);

		$mock_db_access = $this->getMock('DbAccessor');
		$mock_db_access->expects($this->once())
				->method('db_query_select')
				->with(array(
							array('*'),
							array('*'),
							array(),
							"M1.nick AS nick_author, M2.nick AS nick_editor"), 
				   array('ITEM','AD','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), 
				   "ITEM.item_id = $expected_id AND ITEM.item_id = AD.item_id AND ITEM.author = M1.user_id AND ITEM.editor = M2.user_id")
				->will($this->returnValue($expected_sql_query));
		$mock_db_access->expects($this->once())
				->method('db_do_query')
				->with($expected_sql_query)
				->will($this->returnValue($expected_sql_result));
		$mock_db_access->expects($this->once())
				->method('db_num_rows')
				->with($expected_sql_result)
				->will($this->returnValue(1));
		$mock_db_access->expects($this->once())
				->method('db_fetch_array')
				->with($expected_sql_result)
				->will($this->returnValue($expected_ad));
		$ad_page->set_db_accessor($mock_db_access);

		$mock_error_nav = $this->getMock('ErrorNavigation');
		$mock_error_nav->expects($this->never())
				->method('errorpage');
		$ad_page->set_error_navigation($mock_error_nav);

		$mock_item_functions = $this->getMock('ItemFunctions');
		$mock_item_functions->expects($this->once())
				->method('item_page_inc_execute')
				->with($this->equalTo($page, 'ad'));
		$ad_page->set_item_functions($mock_item_functions);

		$ad_page->page_inc_execute($page);

		$this->assertEquals($expected_title, $page->content['title']);
		$this->assertEquals($expected_author, $page->content['owner']);
	}

	function test_page_inc_dress() {
		$expected_ad_body = 'expected ad body';
		$expected_desqlized_ad_body = 'expected desqlized ad body';
		$expected_ad_region = 'expected ad region';
		$expected_desqlized_ad_region = 'expected desqlized ad region';

		$page = new Page();

		$page->content['ad']['body'] = $expected_ad_body;
		$page->content['ad']['region'] = $expected_ad_region;

		$ad_page = new AdPage();

		$mock_item_functions = $this->getMock("ItemFunctions");
		$mock_item_functions->expects($this->once())
				->method("item_page_inc_dress_before")
				->with($this->isInstanceOf('Page'));
		$mock_item_functions->expects($this->once())
				->method("item_page_inc_dress_after")
				->with($this->isInstanceOf('Page'));
		$ad_page->set_item_functions($mock_item_functions);

		$mock_db_access = $this->getMock("DbAccessor");
		$mock_db_access->expects($this->once())
				->method("db_desqlize")
				->with($expected_ad_body)
				->will($this->returnValue($expected_desqlized_ad_body));
		$ad_page->set_db_accessor($mock_db_access);

		$mock_region_manager = $this->getMock("RegionManager");
		$mock_region_manager->expects($this->once())
				->method('region_format')
				->with($expected_ad_region)
				->will($this->returnValue($expected_desqlized_ad_region));
		$ad_page->set_region_manager($mock_region_manager);

		$ad_page->page_inc_dress($page);

		$this->assertEquals($expected_desqlized_ad_body, $page->content['ad']['body']);
		$this->assertEquals($expected_desqlized_ad_region, $page->content['ad']['formatted_region']);
		$this->assertTrue($page->content['ad']['passed']);
	}
}

function print_ad($page, $ad) {
	$page->content['ad']['passed'] = true;
}

?>
